package com.iotechn.unimall.app.api.integralactivity;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.biz.service.integral.IntegralBizService;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.domain.IntegralDetailDO;
import com.iotechn.unimall.data.domain.IntegralUserDO;
import com.iotechn.unimall.data.dto.UserDTO;
import com.iotechn.unimall.data.dto.integralactivity.IntegralDetailListDTO;
import com.iotechn.unimall.data.enums.IntegralSonType;
import com.iotechn.unimall.data.enums.IntegralType;
import com.iotechn.unimall.data.enums.intergal.IntergalOperType;
import com.iotechn.unimall.data.mapper.IntegralDetailMapper;
import com.iotechn.unimall.data.mapper.IntegralUserMapper;
import com.iotechn.unimall.data.util.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by rize on 2019/7/14.
 */
@Service
public class IntegralDetailServiceImpl implements IntegralDetailService {

    @Autowired
    private IntegralBizService integralBizService;
    @Autowired
    private IntegralDetailMapper integralDetailMapper;
    @Autowired
    private IntegralUserMapper integralUserMapper;


    /**
     * 获取积分列表
     * @param userId
     * @return
     * @throws ServiceException
     */
    public List<IntegralDetailDO> detailList(Integer type,Long userId) throws ServiceException{
        UserDTO userDTO=SessionUtil.getUser();
        Wrapper<IntegralDetailDO> wrapper = new EntityWrapper<IntegralDetailDO>();
        wrapper.eq("type", type);
        wrapper.eq("user_id", userDTO.getId());
        List<IntegralDetailDO> integralDetailDOS = integralDetailMapper.selectList(wrapper);
        return integralDetailDOS;
    }
    /**
     * 获取兑换列表
     * @param userId
     * @return
     * @throws ServiceException
     */
    public List<IntegralDetailDO> chargeList(Integer type,Long userId) throws ServiceException{
        UserDTO userDTO=SessionUtil.getUser();
        Wrapper<IntegralDetailDO> wrapper = new EntityWrapper<IntegralDetailDO>();
        wrapper.eq("type", 2);
//        wrapper.eq("son_type", type);
        wrapper.eq("user_id", userDTO.getId());
        List<IntegralDetailDO> integralDetailDOS = integralDetailMapper.selectList(wrapper);
        return integralDetailDOS;
    }



    /**
     * 分享送积分
     * @param userId
     * @return
     * @throws ServiceException
     */
    public Boolean shareIntegral(Long userId
    ) throws ServiceException{
         UserDTO userDTO=SessionUtil.getUser();
        Wrapper<IntegralDetailDO> wrapper = new EntityWrapper<IntegralDetailDO>();
        wrapper.where("to_days(gmt_create)=to_days(now())");
        wrapper.eq("type", 1);
        wrapper.eq("user_id", userDTO.getId());
        wrapper.eq("son_type", IntegralSonType.SHARE.getCode());
        List<IntegralDetailDO> integralDetailDOS = integralDetailMapper.selectList(wrapper);
        if(integralDetailDOS!=null && integralDetailDOS.size()>0){
            return false;
            //throw new AppServiceException(ExceptionDefinition.COUPON_STRATEGY_INCORRECT);
        }
        return integralBizService.addIntegralDetailAdd(userDTO.getId(), IntegralType.EARN.getCode(), IntegralSonType.SHARE.getCode(),0L,IntegralSonType.SHARE.getMsg());
    }

    @Override
    public Map<String, Object> userIntegralDetails(Long userId) throws ServiceException {
        final String userIntegralKey = "userIntegral";
        final String userObtainKey = "userObtain";
        final String userConsumeKey = "userConsume";
        UserDTO userDTO=SessionUtil.getUser();
        Wrapper<IntegralDetailDO> wrapper = new EntityWrapper<IntegralDetailDO>();
        wrapper.eq("user_id", userDTO.getId());
        wrapper.orderBy("integral_time",false);
        List<IntegralDetailDO> integralDetailDOS = integralDetailMapper.selectList(wrapper);
        Map<Integer, List<IntegralDetailDO>> integralDetailMap = integralDetailDOS.stream().collect(Collectors.groupingBy(IntegralDetailDO::getType));
        IntegralUserDO userIntegral = getUserIntegral(userId);
        Map<String,Object> map = new HashMap<>();
        map.put(userIntegralKey,userIntegral);
        map.put(userObtainKey,integralDetailMap.get(IntergalOperType.OBTAIN.getCode()) == null ? new ArrayList<>():integralDetailMap.get(IntergalOperType.OBTAIN.getCode()));
        map.put(userConsumeKey,integralDetailMap.get(IntergalOperType.CONSUME.getCode()) == null ? new ArrayList<>():integralDetailMap.get(IntergalOperType.CONSUME.getCode()));
        return map;
    }


    public IntegralUserDO getUserIntegral(Long userId){
        Wrapper<IntegralUserDO> wrapper = new EntityWrapper<IntegralUserDO>();
        wrapper.eq("user_id", userId);
        List<IntegralUserDO> integralGoodsDOS = integralUserMapper.selectList(wrapper);
        if(integralGoodsDOS!=null && integralGoodsDOS.size()>0){
            return integralGoodsDOS.get(0);
        }else{
            IntegralUserDO integralUserDO=new IntegralUserDO();
            integralUserDO.setUserId(userId);
            integralUserDO.setCurrentIntegral(0);
            integralUserDO.setTotalIntegral(0);
            integralUserDO.setGmtUpdate(new Date());
            return integralUserDO;
        }
    }


    @Override
    public List<IntegralDetailListDTO> getExchangeRecord(Long userId) throws ServiceException {
        List<IntegralDetailListDTO> integralDetailListDTO = integralDetailMapper.getIntegralDetailListDTO(userId);
        return integralDetailListDTO;
    }
}
